package com.hongxun.budget.ui.activity.ys;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.fragment.ys.YsDeviceBuyInfoFragment;
import com.hongxun.budget.ui.fragment.ys.YsDeviceInfoFragment;
import com.hongxun.budget.ui.fragment.ys.YsInfoFragment;
import com.hongxun.budget.ui.fragment.ys.YsMaterialBuyInfoFragment;
import com.hongxun.budget.ui.fragment.ys.YsProjectContentFragment;

import butterknife.BindView;
import butterknife.OnClick;

public class YsProjectDetailActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.horizontalScrollView)
    HorizontalScrollView horizontalScrollView;
    int select = 0;
    Fragment content, device, deviceBuy, materialBuy, quantities;
    @BindView(R.id.right_save)
    Button rightSave;

    @Override
    public  int getLayoutId() {
        return R.layout.activity_ys_project_detail;
    }

    @Override
    public  void initView(Bundle savedInstanceState) {
        rightSave.setVisibility(View.VISIBLE);
        rightSave.setText("统计信息");
        radioGroup.setOnCheckedChangeListener(this);
        radioGroup.check(R.id.radioButton1);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        scrollCenter(checkedId);
        switch (checkedId) {
            case R.id.radioButton1:
                select = 0;
                break;
            case R.id.radioButton2:
                select = 1;
                break;
            case R.id.radioButton3:
                select = 2;
                break;
            case R.id.radioButton4:
                select = 3;
                break;
            case R.id.radioButton5:
                select = 4;
                break;
        }
        selecte(select);
    }

    void scrollCenter(int i) {
        RadioButton rb = findViewById(i);
        int scrollViewWidth = horizontalScrollView.getWidth();
        int rb_px = (int) rb.getX() + rb.getWidth() / 2;
        horizontalScrollView.scrollTo(rb_px - scrollViewWidth / 2, 0);
    }

    /**
     * 设置fragment显示以及切换
     *
     * @param i
     */
    private void selecte(int i) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction, i);
        switch (i) {
            case 0:
                if (content == null) {
                    content = new YsProjectContentFragment();
                    fragmentTransaction.add(R.id.content, content);
                } else {
                    fragmentTransaction.show(content);
                }
                break;
            case 1:
                if (device == null) {
                    device = new YsDeviceInfoFragment();
                    fragmentTransaction.add(R.id.content, device);
                } else {
                    fragmentTransaction.show(device);
                }
                break;
            case 2:
                if (deviceBuy == null) {
                    deviceBuy = new YsDeviceBuyInfoFragment();
                    fragmentTransaction.add(R.id.content, deviceBuy);
                } else {
                    fragmentTransaction.show(deviceBuy);
                }
                break;
            case 3:
                if (materialBuy == null) {
                    materialBuy = new YsMaterialBuyInfoFragment();
                    fragmentTransaction.add(R.id.content, materialBuy);
                } else {
                    fragmentTransaction.show(materialBuy);
                }
                break;
            case 4:
                if (quantities == null) {
                    quantities = new YsInfoFragment();
                    fragmentTransaction.add(R.id.content, quantities);
                } else {
                    fragmentTransaction.show(quantities);
                }
                break;

        }
        fragmentTransaction.commit();
    }


    private void hideFragment(FragmentTransaction ft, int i) {
        if (content != null && i != 0)
            ft.hide(content);
        if (device != null && i != 1)
            ft.hide(device);
        if (deviceBuy != null && i != 2)
            ft.hide(deviceBuy);
        if (materialBuy != null && i != 3)
            ft.hide(materialBuy);
        if (quantities != null && i != 4)
            ft.hide(quantities);
    }


    @OnClick(R.id.right_save)
    public void onViewClicked() {
        startActivity(new Intent(context, YsStatisticsActivity.class));
    }
}
